package com.hankol.winecellar.help;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baselibrary.bean.ClientStreamTimeline;
import com.baselibrary.bean.VideoDynamicBean;
import com.baselibrary.bean.download.DownloadInfo;
import com.baselibrary.db.DBManager;
import com.baselibrary.manager.CityManager;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.AppManager;
import com.baselibrary.utils.KeyBoardUtils;
import com.baselibrary.utils.NetworkUtil;
import com.baselibrary.utils.SpUtils;
import com.hankol.material.help.MaterialHelp;
import com.hankol.winecellar.utils.BaiduMapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanglijie
 * @description:转换对象
 * @date : 2021/05/27
 */
@Route(path = ActivityRouter.ACTIVITY.NATIVE_HELP)
public class NativeHelpIm implements NativeHelp, MaterialHelp {

    @Override
    public void init(Context context) {

    }

    @Override
    public void toNative(AppCompatActivity mAct, String query, String latitude, String longitude) {
//        LocationUtil.toNative(mAct, query, latitude, longitude);
        BaiduMapUtils.INSTANCE.openNav(mAct, 0.0, 0.0, "当前位置", Double.parseDouble(latitude), Double.parseDouble(longitude), query);
    }

    public static VideoDynamicBean setSLDData(Object o) {
        VideoDynamicBean mVideoDynamicBean = new VideoDynamicBean();
        List<VideoDynamicBean.AtMembersBean> mVideoDynamicAtMembersBeans = new ArrayList<>();
        VideoDynamicBean.TopicBean mVideoDynamicTopicBean = new VideoDynamicBean.TopicBean();
        VideoDynamicBean.UserBean mVideoDynamicUserBean = new VideoDynamicBean.UserBean();
        if (o instanceof ClientStreamTimeline) {//新版首页跳转
            return setHomeDynammic(o, mVideoDynamicBean, mVideoDynamicAtMembersBeans, mVideoDynamicTopicBean, mVideoDynamicUserBean);
        }
        return null;
    }

    /**
     * 转化主页数据
     */
    private static VideoDynamicBean setHomeDynammic(Object o, VideoDynamicBean mVideoDynamicBean, List<VideoDynamicBean.AtMembersBean> mVideoDynamicAtMembersBeans, VideoDynamicBean.TopicBean mVideoDynamicTopicBean, VideoDynamicBean.UserBean mVideoDynamicUserBean) {
        if (o instanceof ClientStreamTimeline) {
            List<ClientStreamTimeline.SimpleUser> atMembers = ((ClientStreamTimeline) o).getAtMembers();
            if (atMembers != null) {
                for (ClientStreamTimeline.SimpleUser atMember : atMembers) {
                    VideoDynamicBean.AtMembersBean atMembersBean = new VideoDynamicBean.AtMembersBean();
                    atMembersBean.name = atMember.getName();
                    atMembersBean.icon = atMember.getIcon();
                    atMembersBean.focusFans = atMember.isFocusFans();
                    atMembersBean.id = atMember.getId();
                    mVideoDynamicAtMembersBeans.add(atMembersBean);
                }
            }
            List<Integer> tagIds = ((ClientStreamTimeline) o).getTagIds();
            List<String> tags = ((ClientStreamTimeline) o).getTags();
            if (tagIds != null && tags != null) {
                if (tags.size() > 0 && tagIds.size() > 0) {
                    mVideoDynamicTopicBean.topicName = tags.get(0);
                    mVideoDynamicTopicBean.topicId = tagIds.get(0);
                }
            }
            ClientStreamTimeline.SimpleUser user = ((ClientStreamTimeline) o).getUser();
            mVideoDynamicUserBean.id = user.getId();
            mVideoDynamicUserBean.icon = user.getIcon();
            mVideoDynamicUserBean.name = user.getName();
            mVideoDynamicUserBean.focusFans = user.isFocusFans();


            mVideoDynamicBean.giveCount = ((ClientStreamTimeline) o).getSupportNum();
            mVideoDynamicBean.likeCount = ((ClientStreamTimeline) o).getFavoriteNum();
            mVideoDynamicBean.shareCount = (((ClientStreamTimeline) o).getForwardNum() + ((ClientStreamTimeline) o).getOutForwardNum());


            mVideoDynamicBean.atMembersBean = mVideoDynamicAtMembersBeans;
            mVideoDynamicBean.userBean = mVideoDynamicUserBean;
            mVideoDynamicBean.topicBeans = mVideoDynamicTopicBean;


            mVideoDynamicBean.location = ((ClientStreamTimeline) o).getAddress();
            mVideoDynamicBean.des = ((ClientStreamTimeline) o).getDesc();
            return mVideoDynamicBean;
        }
        return null;
    }

    @Override
    public String distance(double lat, double log) {
        return "";

    }

    @SuppressLint("NewApi")
    @Override
    public void showCancelPop(AppCompatActivity activity, TextView sAgencyStatus, LinearLayout mLlCancel, int id, int enlistPeopleNumPass) {
    }

    @Override
    public void saveHistory(String content) {
        DBManager.getInstance().insertSearchContent(SpUtils.getInstance().getUserId(), content);
    }

    @Override
    public void cleanHistory(int userId) {
        DBManager.getInstance().deleteSearchHistory(userId);
    }

    //素材库搜索 -获取
    @Override
    public List<String> getSearchHistory(int userId) {
        return DBManager.getInstance().querySearchHistory4Material(userId);
    }

    //素材库搜索 -保存
    @Override
    public void saveSearchHistory(int userId, String content) {
        DBManager.getInstance().insertSearchContent4Material(userId, content);
    }

    //素材库搜索 -清除
    @Override
    public void cleanSearchHistory(int userId) {
        DBManager.getInstance().deleteSearchHistory4Material(userId);
    }

    //订单搜索 -保存
    @Override
    public void saveHistory4Order(Boolean isManager, int userId, String content) {
        if (isManager) {
            DBManager.getInstance().insertSearchContent4ManagerOrder(userId, content);
        } else {
            DBManager.getInstance().insertSearchContent4Order(userId, content);
        }
    }

    //订单搜索 -获取
    @Override
    public List<String> getHistory4Order(Boolean isManager, int userId) {
        if (isManager) {
            return DBManager.getInstance().querySearchHistory4ManagerOrder(userId);
        } else {
            return DBManager.getInstance().querySearchHistory4Order(userId);
        }
    }

    //订单搜索 -清除
    @Override
    public void cleanHistory4Order(Boolean isManager, int userId) {
        if (isManager) {
            DBManager.getInstance().deleteSearchHistory4ManagerOrder(userId);
        } else {
            DBManager.getInstance().deleteSearchHistory4Order(userId);
        }
    }

    @Override
    public boolean isWifiConnected(Context context) {
        return NetworkUtil.isWifiConnected(context);
    }

    @Override
    public void hideShowKeyboard(Activity context) {
        KeyBoardUtils.hideSoftKeyboard(context);
    }

    /**
     * 获取下载列表
     *
     * @author: merbng
     * @time: 2021/11/3 9:18
     * @description:
     */
    @Override
    public List<DownloadInfo> getDownloadList(int userId, Boolean isDownloading) {
        return DBManager.getInstance().getDownloadList(userId, isDownloading);
    }

    @Override
    public void saveDownload(int userId, DownloadInfo info) {
        DBManager.getInstance().saveDownload(userId, info);
    }

    @Override
    public void updateDownload(DownloadInfo info) {
        DBManager.getInstance().updateDownload(info);
    }

    @Override
    public void deleteDownload(int userId, String url) {
        DBManager.getInstance().deleteDownload(userId, url);
    }

    @Override
    public void login() {
        AppManager.getAppManager().openLoginPage();
    }

    @Override
    public String getCity(String city) {
        return CityManager.getInstance().gtCityName(city);
    }

    @Override
    public String getAreaName(String area) {
        return CityManager.getInstance().gtAreaName(area);
    }

    @Override
    public void shareMaterial() {

    }

}
